package client.comm.baoding.adapter.main

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.AllCate
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.Category
import client.comm.baoding.api.bean.HomeCateRet
import client.comm.baoding.databinding.ItemCateCateGoodBinding
import client.comm.baoding.databinding.ItemHomeCateGoodBinding
import client.comm.commlib.base.BaseAdapter
import client.comm.commlib.base.BindingViewHoder
import com.bumptech.glide.RequestManager

class CateAllCateAdapter : BaseAdapter<Category> {

    fun itemClick(bean: Category, p: Int) {
//        context.startActivity(Intent(context, JyCateListActivity::class.java).apply {
//            putExtra("bean", bean)
//        })
        cateItemCli?.invoke(bean, p)
    }

    var cateItemCli: ((Category, Int) -> Unit)? = null

    val manager: RequestManager

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }
    //设置商品label标签

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate<ViewDataBinding?>(
                layoutInflater,
                R.layout.item_cate_cate_good,
                parent,
                false
            ).apply {

                val itemBinding = this as ItemCateCateGoodBinding
                itemBinding.ivGood.post {
                    val p = itemBinding.ivGood.layoutParams
                    p.height =  itemBinding.ivGood.measuredWidth * 2 / 3
                    p.width = itemBinding.ivGood.measuredWidth * 2 / 3
                    itemBinding.ivGood.layoutParams = p
                }
            }
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as ItemCateCateGoodBinding
        val bean = getItem(position)

        binding.bean = bean
        binding.event = this
        binding.crrIndex = position
        manager.load(bean.small_img).error(R.mipmap.ic_launcher).into(binding.ivGood)
    }

}